<template>

  <div class="app-container">
    <div id="water">
      <canvas id="canvas"></canvas>
    </div>


    <el-table v-loading="loading" :data="WatermarkConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="是否全局" align="center" prop="isall" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.watermark_setting" :value="scope.row.isall"/>
        </template>
      </el-table-column>
      <el-table-column label="是否包含员工号" align="center" prop="usercode" :formatter="usercodeFormat" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.usercode"/>
        </template>
      </el-table-column>
      <el-table-column label="是否包含员工姓名" align="center" prop="username" :formatter="usernameFormat" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.username"/>
        </template>
      </el-table-column>
      <el-table-column label="是否包手机号" align="center" prop="istel" :formatter="istelFormat" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.istel"/>
        </template>
      </el-table-column>
      <el-table-column label="是否包当前日期" align="center" prop="watermarkdate" :formatter="watermarkdateFormat" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.watermarkdate"/>
        </template>
      </el-table-column>
      <el-table-column label="是否包含组织名称" align="center" prop="orgname" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.orgname"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:WatermarkConfig:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水印设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="是否全局">
          <el-radio-group v-model="form.isall">
            <el-radio
              v-for="dict in isallOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否包含员工号">
          <el-radio-group v-model="form.usercode">
            <el-radio
              v-for="dict in usercodeOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否包含员工姓名">
          <el-radio-group v-model="form.username">
            <el-radio
              v-for="dict in usernameOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否包手机号">
          <el-radio-group v-model="form.istel">
            <el-radio
              v-for="dict in istelOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否包当前日期">
          <el-radio-group v-model="form.watermarkdate">
            <el-radio
              v-for="dict in watermarkdateOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否包含组织名称">
          <el-radio-group v-model="form.orgname">
            <el-radio
              v-for="dict in orgnameOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<!--<script src="@{/api/watermark.js}"></script>-->
<script>
import {addWatermarkConfig, delWatermarkConfig, exportWatermarkConfig, getWatermarkConfig, listWatermarkConfig, updateWatermarkConfig} from "@/api/system/WatermarkConfig";
import {getNow} from "@/api/watermark";

export default {
  name: "WatermarkConfig",
  dicts: ['watermark_setting','sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水印设置表格数据
      WatermarkConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否全局字典
      isallOptions: [],
      // 是否包含员工号字典
      usercodeOptions: [],
      // 是否包含员工姓名字典
      usernameOptions: [],
      // 是否包手机号字典
      istelOptions: [],
      // 是否包当前日期字典
      watermarkdateOptions: [],
      // 是否包含组织名称字典
      orgnameOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isall: null,
        usercode: null,
        username: null,
        istel: null,
        watermarkdate: null,
        orgname: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    //获取当前时间
    var now = getNow();
    //watermark();
    this.getList();
    this.getDicts("watermark_setting").then(response => {
      this.isallOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.usercodeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.usernameOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.istelOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.watermarkdateOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.orgnameOptions = response.data;
    });
  },
  methods: {
    /** 查询水印设置列表 */
    getList() {
      this.loading = true;
      listWatermarkConfig(this.queryParams).then(response => {
        this.WatermarkConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否全局字典翻译
    isallFormat(row, column) {
      return this.selectDictLabel(this.isallOptions, row.isall);
    },
    // 是否包含员工号字典翻译
    usercodeFormat(row, column) {
      return this.selectDictLabel(this.usercodeOptions, row.usercode);
    },
    // 是否包含员工姓名字典翻译
    usernameFormat(row, column) {
      return this.selectDictLabel(this.usernameOptions, row.username);
    },
    // 是否包手机号字典翻译
    istelFormat(row, column) {
      return this.selectDictLabel(this.istelOptions, row.istel);
    },
    // 是否包当前日期字典翻译
    watermarkdateFormat(row, column) {
      return this.selectDictLabel(this.watermarkdateOptions, row.watermarkdate);
    },
    // 是否包含组织名称字典翻译
    orgnameFormat(row, column) {
      return this.selectDictLabel(this.orgnameOptions, row.orgname);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        isall: "0",
        usercode: "0",
        username: "0",
        istel: "0",
        watermarkdate: "0",
        orgname: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加水印设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWatermarkConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改水印设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWatermarkConfig(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWatermarkConfig(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除水印设置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWatermarkConfig(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有水印设置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWatermarkConfig(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    getWater() {
      const canvas = document.getElementById("canvas");//获取canvas
      canvas.width = 200;//设置画布宽度
      canvas.height = 200;//设置画布高度
      canvas.style.display = "none";//隐藏画布本身
      const ctx = canvas.getContext("2d");//获取画笔
      ctx.font = "16px";设置文字大小
      ctx.fillStyle = "rgba(0,0,0,.3)";//设置文字颜色及透明度
      ctx.rotate(-0.4);//设置文字旋转角度
      ctx.fillText("这是水印", canvas.width / 6, canvas.height / 2);//设置显示文字内容
      const img = canvas.toDataURL("image/png");//参数默认为 image/png,可以是其他image/jpeg等，该方法返回值是一个url，是base64组成的图片的源数据、可以直接赋值给图片的src属性
      const style = `background-image:url(${img});`;//定义样式
      water.setAttribute("style", style);//给要添加水印的元素设置样式
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
#water{
  width: 100%;
  height: 100%;
  position: absolute;
  left:0;
  top:0;
  pointer-events: none;
}

</style>
